<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="geometry" content="line">
  <link rel="stylesheet" href="./assets/common.css">
  <title>Line Plot with Time</title>
</head>

<body>
  <div id="mountNode"></div>
  <script src="../build/g2.js"></script>
  <script>
    const data = [];
    const chart = new G2.Chart({
      container: 'mountNode',
      forceFit: true,
      height: window.innerHeight
    });
    chart.source(data, {
      time: {
        alias: '时间',
        type: 'time',
        mask: 'MM:ss',
        tickCount: 10,
        nice: false
      },
      temperature: {
        alias: '平均温度(°C)',
        min: 10,
        max: 35
      },
      type: {
        type: 'cat'
      }
    });
    chart.line()
      .position('time*temperature')
      .color('type', [ '#ff7f0e', '#2ca02c' ])
      .shape('smooth')
      .size(2);
    chart.render();
    setInterval(function() {
      const now = new Date();
      const time = now.getTime();
      const temperature1 = ~~(Math.random() * 5) + 22;
      const temperature2 = ~~(Math.random() * 7) + 17;
      if (data.length >= 200) {
        data.shift();
        data.shift();
      }
      data.push({
        time,
        temperature: temperature1,
        type: '记录1'
      });
      data.push({
        time,
        temperature: temperature2,
        type: '记录2'
      });
      chart.changeData(data);
    }, 1000);
  </script>
</body>

</html>
